package com.dd.ms.auth.service.impl;

import com.dd.ms.auth.pojo.DO.DeptDO;
import com.dd.ms.auth.pojo.DO.DeptUsersRelDO;
import com.dd.ms.auth.pojo.DO.UserDO;
import com.dd.ms.auth.pojo.req.DeptUsesRelAddReq;
import com.dd.ms.auth.repository.DeptRepository;
import com.dd.ms.auth.repository.DeptUsersRelRepository;
import com.dd.ms.auth.repository.UserRepository;
import com.dd.ms.auth.service.DeptUsersRelService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author liuxianmeng
 * @CreateTime 2025/7/9 16:03
 * @Description 用户角色关联操作
 */
@Service
@RequiredArgsConstructor
public class DeptUsersRelServiceImpl implements DeptUsersRelService {

    private final DeptUsersRelRepository deptUsersRelRepository;
    private final DeptRepository deptRepository;
    private final UserRepository userRepository;

    @Transactional(rollbackFor = Exception.class)
    public void addDeptUsersRel(DeptUsesRelAddReq req) {
        DeptDO deptDO = deptRepository.get(req.getDeptIdentification());
        UserDO userDO = userRepository.getUserByAccount(req.getAccount());
        deptUsersRelRepository.add(
                DeptUsersRelDO.builder()
                        .userId(userDO.getUserId())
                        .deptId(deptDO.getDeptId())
                        .build()
        );
    }
}
